# coding=utf-8

import collections
import random
import questions

from question import Question, QuestionSet, QuestionGroup

def gen_add(n):
    sum = int(random.random() * (n+1))
    n1 = int(random.random() * (sum+1))
    n2 = sum - n1
    sums = []
    diffs = [-10, -3, -2, -1, 1, 2, 3, 10]

    for diff in diffs:
        res = diff + sum
        if res >=0 and res <= n:
            sums.append(res)

    sums = questions.random_items(sums, 3)
    sums.append(sum)
    sums = questions.random_items(sums)

    assert len(sums) == 4

    return ("%d + %d = ?" % (n1, n2), str(sums[0]), str(sums[1]), str(sums[2]), str(sums[3]), sums.index(sum))


def gen_multi(n1, n2):
    # sum = int(random.random() * (n+1))
    # n1 = int(random.random() * (sum+1))
    # n2 = sum - n1
    sum = n1 * n2
    sums = []
    diffs = [-10, -3, -2, -1, 1, 2, 3, 10]

    for diff in diffs:
        res = diff + sum
        if res >=0:# and res <= n:
            sums.append(res)

    sums = questions.random_items(sums, 3)
    sums.append(sum)
    sums = questions.random_items(sums)

    assert len(sums) == 4

    return ("%d * %d = ?" % (n1, n2), str(sums[0]), str(sums[1]), str(sums[2]), str(sums[3]), sums.index(sum))


def gen_div(n1, n2):
    # sum = int(random.random() * (n+1))
    # n1 = int(random.random() * (sum+1))
    # n2 = sum - n1
    sum = n1 * n2
    n1, n2, sum = sum, n1, n2


    sums = []
    diffs = [-10, -3, -2, -1, 1, 2, 3, 10]

    for diff in diffs:
        res = diff + sum
        if res >=0:# and res <= n:
            sums.append(res)

    sums = questions.random_items(sums, 3)
    sums.append(sum)
    sums = questions.random_items(sums)

    assert len(sums) == 4

    return ("%d / %d = ?" % (n1, n2), str(sums[0]), str(sums[1]), str(sums[2]), str(sums[3]), sums.index(sum))


def gen_ques(num, category = 20):# swap = False, option_count = 4):
    rs = []
    for i in range(num):
        rs.append(gen_add(category))

    return rs


def test(num = 20, category = 150):
    ques_set = QuestionSet("20 以内加法")
    for i in range(num):
        ques = gen_add(category)
        q = Question(ques[0], [ques[1], ques[2], ques[3], ques[4]], ques[5])
        ques_set.AddQuestion(q)

    ques_grp = QuestionGroup("20 以内加法")
    ques_grp.AddQuestionSet(ques_set)

    return ques_grp


def test_multi(num = 20, category = 150):
    ques_set = QuestionSet("乘法口决表：乘法")
    for i in range(1, 10):
        for j in range(1, 10):
            ques = gen_multi(i, j)#category)
            q = Question(ques[0], [ques[1], ques[2], ques[3], ques[4]], ques[5])
            ques_set.AddQuestion(q)

    ques_grp = QuestionGroup("乘法口决表：乘法")
    ques_grp.AddQuestionSet(ques_set)

    return ques_grp


def test_div(num = 20, category = 150):
    ques_set = QuestionSet("乘法口决表：除法")
    for i in range(1, 10):
        for j in range(1, 10):
            ques = gen_div(i, j)#category)
            q = Question(ques[0], [ques[1], ques[2], ques[3], ques[4]], ques[5])
            ques_set.AddQuestion(q)

    ques_grp = QuestionGroup("乘法口决表：除法")
    ques_grp.AddQuestionSet(ques_set)

    return ques_grp


if __name__ == "__main__":
    import question
    ques_grp = test()
    ques_grp = test_multi()
    ques_grp = test_div()
    question.showQuestions(ques_grp.GetQuestions())

